package com.donleo.security.service.impl;

import com.donleo.security.common.CommonResult;
import com.donleo.security.dao.MyUserDao;
import com.donleo.security.model.MyPermission;
import com.donleo.security.model.MyUser;
import com.donleo.security.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author liangd
 * date 2020-12-08 15:09
 * code 自定义实现UserDetailsService接口
 */
@Service
public class MyUserServiceImpl implements MyUserService {

    @Autowired
    private MyUserDao myUserDao;

    @Override
    public MyUser getUserByName(String name) {
        List<MyUser> users= myUserDao.getUserByName(name);
        Assert.isTrue(users.size()==1,"您输入的账户不存在，或者有多个相同的账户");
        return users.get(0);
    }

    @Override
    public List<MyPermission> getPermissionsByUserId(Integer id) {
        return myUserDao.getPermissionsByUserId(id);
    }

    @Override
    public CommonResult findAll() {
        List<MyUser> list = myUserDao.findAll();
        return CommonResult.success(list);
    }

    @Override
    public CommonResult findById(Integer id) {
        MyUser myUser = myUserDao.findById(id);
        return CommonResult.success(myUser);
    }

   /*
    //实现security自带的UserDetailsService接口
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //设置权限集合
        List<GrantedAuthority> roles = AuthorityUtils.commaSeparatedStringToAuthorityList("roles");
        //
        return new User("liangd",
                new BCryptPasswordEncoder().encode("123456"),roles);
    }*/
}
